package com.linzm.leetcode.primary.哈希表.两个数组的交集;

import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author zimingl
 * @Date 2022/10/6 23:34
 * @Description: TODO
 */
public class 哈希表第202题快乐数 {

    public boolean isHappy(int n) {
        Set<Integer> record = new HashSet<>();
        while (n != 1 && !record.contains(n)) {
            record.add(n);
            n = getNextNumber(n);
        }
        return n == 1;
    }

    private int getNextNumber(int n) {
        int res = 0;
        while (n > 0) {
            int temp = n % 10;
            res += temp * temp;
            n = n / 10;
        }
        return res;
    }

    @Test
    public void test(){
        int n = 19;
        isHappy(n);
    }
}
